#include "socket_glue_code.h"

#ifdef WIN32 
//Win32 glue code
int read (int sd, char* buf, int len) {       
    return recv(sd, buf, len, 0);
}

int write (int sd, const char* buf, int len){
    return send(sd, buf, len, 0);
}

void close(int sd) {
    closesocket(sd);
}

int startWinsock()
{
    WSADATA wsa;
    int wsacode =  WSAStartup(MAKEWORD(2,0),&wsa);	
    printf("WSA Startup %d\n" ,wsacode);
    return wsacode;
}





void sleep(long duration_in_seconds){
    Sleep(duration_in_seconds * 1000);
}
#endif



void printWindowsSocketError()
{
#ifdef WIN32
    printf("Fehler code: %d\n",WSAGetLastError());
#endif
}

void stopWin32(){
#ifdef WIN32
    WSACleanup();
#endif
}

//Win32 starting Routines
void startWin32(){
#ifdef WIN32
    long rc;

    rc=startWinsock();

    if(rc!=0)
    {
        printf("Fehler: startWinsock, fehler code: %d\n",rc);
    }
    else
    {
        printf("Winsock gestartet!\n");
    }
#endif
}

//reads from socket connection
std::string read_socket(int sd) {
    int cnt;
    int portion = 100;
    std::string input;
    do {
        char *buf = new char[portion];
        cnt = read(sd,(char*) buf, portion);
        if (cnt >= 0)
            input.append(buf, cnt);
    } while (cnt==portion);
    return input;
}
